package com.prs.crm.action.project;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.project.Task;
import com.prs.crm.domain.project.TaskCritique;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.project.TaskCritiqueService;
import com.prs.crm.service.project.TaskService;

public class TaskCritiqueAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private TaskService taskService;
	
	private TaskCritiqueService taskCritiqueService;
	
	private TaskCritique critique;
	
	private File upload;
	
	private String uploadFileName;
	
	private Task task;
	
	private Person currentUser;
	
	private PaginationSupport pagination;

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskCritiqueService(TaskCritiqueService taskCritiqueService) {
		this.taskCritiqueService = taskCritiqueService;
	}

	public TaskCritiqueService getTaskCritiqueService() {
		return taskCritiqueService;
	}

	public void setCritique(TaskCritique critique) {
		this.critique = critique;
	}

	public TaskCritique getCritique() {
		return critique;
	}

	public void setCurrentUser(Person currentUser) {
		this.currentUser = currentUser;
	}

	public Person getCurrentUser() {
		return currentUser;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}
	
	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}
	
	@Action("listTaskCritique")
	public String listTaskCritique()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(TaskCritique.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTaskService().listTaskCritique(criteria, pagination,
				task));
		return SUCCESS;
	}
	
	@Action("viewTaskCritique")
	public String viewTaskCritique()
	{
		currentUser = AppContext.getAccountContext().currentUser();
		return SUCCESS;
	}
	
	@Action("createTaskCritique")
	public String createTaskCritique()
	{
		return SUCCESS;
	}
	
	@Action("listMyCritique")
	public String listMyCritique()
	{
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTaskCritiqueService().listMyCritique(pagination, 
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}
	
	@Action("editMyCritique")
	public String editMyCritique()
	{
		this.setTask(this.getCritique().getTask());
		return SUCCESS;
	}
	
	@Action("listVaryCritique")
	public String listVaryCritique()
	{
		return this.listMyCritique();
	}
	
	@Action("viewCritique")
	public String viewCritique()
	{
		return this.editMyCritique();
	}
	
	@Clew(results={"success"})
	@Action(value="modifyCritique",results={@Result(name="success",type="redirect",location="listVaryCritique")
			,@Result(name="error",type="chain",location="editMyCritique")})
	public String modifyCritique()
	{
		try
		{
			this.getTaskCritiqueService().updateCritique(critique,upload,uploadFileName);
			this.setPrompt("修改成功");
			return SUCCESS;
		}
		catch (InValidException e){
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}
	
	@Action(value="saveTaskCritique",results={@Result(name="success",type="redirect"
		,location="listTaskCritique?task=${task.id}"),@Result(name="error"
			,location="/project/createTaskCritique.ftl")})
	public String saveTaskCritique()
	{
		try
		{
			this.getTaskCritiqueService().saveCritique(critique, task,upload,uploadFileName);
			return SUCCESS;
		}
		catch (InValidException e){
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}
	
	@Action(value="removeTaskCritique",results={@Result(name="success",type="redirect"
				,location="listTaskCritique?task=${task.id}")})
	public String removeTaskCritique()
	{
		this.getTaskCritiqueService().removeCritique(task,critique, 
				AppContext.getAccountContext().currentUser());
		return SUCCESS;
	}
}
